package com.southminority.ethnic.controller;

import com.southminority.ethnic.pojo.TestExcelPojo;
import com.southminority.ethnic.pojo.User;
import com.southminority.ethnic.until.ExcelUntil;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Tag(name = "测试模块",description ="测试模块" )
@RestController
@RequestMapping("/test")
public class testController {
    @PostMapping("/export/excel")
    public ResponseEntity<byte[]> exportExcel(
//            @RequestBody List<?> dataList,
//            @RequestParam(defaultValue = "export_data") String fileName
    ) {
        try {
            List<TestExcelPojo> dataList = new ArrayList<>();
            TestExcelPojo testExcelPojo = new TestExcelPojo();
            testExcelPojo.setName("张三");
            testExcelPojo.setAge("20");
            testExcelPojo.setSex("男");
            testExcelPojo.setPhone("13800000000");
            dataList.add(testExcelPojo);

            testExcelPojo=new TestExcelPojo();
            testExcelPojo.setName("李四");
            dataList.add(testExcelPojo);
            byte[] excelBytes = ExcelUntil.exportExcel(dataList);

            String fileName = "output_excel";
            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            // 处理中文文件名
            String encodedFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xlsx";
            headers.setContentDispositionFormData("attachment", encodedFileName);

            return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    @PostMapping("/getMb")
    public ResponseEntity<byte[]> mbExcel()
    {
        try {

            String fileName = "input_excel";

            byte[] excelBytes = ExcelUntil.mbExcel(TestExcelPojo.class);

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            // 处理中文文件名
            String encodedFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xlsx";
            headers.setContentDispositionFormData("attachment", encodedFileName);

            return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 导入Excel并保存到数据库
     */
    @PostMapping("/import/excel")
    public ResponseEntity<String> importAndSave(@RequestParam("file") MultipartFile file) {
        try {
            // 1. 解析Excel为实体类集合（以User为例，根据实际实体类修改）
            List<TestExcelPojo> userList = ExcelUntil.importExcel(file, TestExcelPojo.class);

            // 2. 保存到数据库（通过服务层）
            //userService.saveAll(userList);

            return ResponseEntity.ok("导入成功，共" + userList.size() + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("导入失败：" + e.getMessage());
        }
    }
}
